package com.steel.yfmall.cloud.assistant.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.steel.yfmall.cloud.assistant.api.stock.CqUserPushServiceAPI;
import com.steel.yfmall.cloud.assistant.bean.param.APIParamUserPushBean;
import com.steel.yfmall.cloud.assistant.po.view.OutCallApiLogBean;
import com.steel.yfmall.cloud.assistant.utils.kd.CosmicLoginUtil;
import com.steel.yfmall.cloud.components.mq.bean.RequestSendMQBean;
import com.steel.yfmall.cloud.components.mq.service.MqSendService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @program: CqUserPushServiceAPIImpl
 * @author: liuzhijun
 * @date: 2021-04-21 09:01
 **/
@Service
@Slf4j
public class CqUserPushServiceAPIImpl implements CqUserPushServiceAPI {


    @Override
    public void pushUserToCq(APIParamUserPushBean apiParamUserPushBean) {
        if (apiParamUserPushBean == null) {
            log.info("pushUserToCq: 参数不能为空");
            return;
        }
        if (StringUtils.isBlank(apiParamUserPushBean.getCompnayName()) || StringUtils.isBlank(apiParamUserPushBean.getMainAccount())
                || apiParamUserPushBean.getSubAccount() == null || StringUtils.isBlank(apiParamUserPushBean.getMobile())) {
            log.info("pushUserToCq: 公司名称、主账号、子账号、手机号 都不能为空");
            return;
        }

        String result = "";
        String url = "";
        try {
            //获取登录token
            String accessToken = CosmicLoginUtil.getAccessToken(CosmicLoginUtil.GetAPPToken());
            url = DS_COSMIC_USERBASE_PUSH + "?access_token=" + accessToken;
            String str = "";
            JSONObject obj = new JSONObject();
//            obj.put("data",apiParamUserPushBean);
            obj.put("mobile", apiParamUserPushBean.getMobile());
            obj.put("compnayName", apiParamUserPushBean.getCompnayName());
            obj.put("mainAccount", apiParamUserPushBean.getMainAccount());
            obj.put("subAccount", apiParamUserPushBean.getSubAccount());
            str = obj.toString();
            JSONObject pushData = new JSONObject();
            pushData.put("data",obj);

            result = CosmicLoginUtil.GetHttpMsg(pushData.toString(), url);
            log.info(result);
            //记录日志
            OutCallApiLogBean outCallApiLogBean = new OutCallApiLogBean();
            outCallApiLogBean.setApiUrl(url);
            outCallApiLogBean.setParaText(pushData.toString());
            outCallApiLogBean.setInterfaceMethod(url);
            outCallApiLogBean.setInterfaceMethodName("电商用户子账户信息推送苍穹");
            outCallApiLogBean.setResultText(result);
            RequestSendMQBean requestSendMQBean = new RequestSendMQBean();
            requestSendMQBean.setSendMqTitle("insert_out_call_api_log");
            requestSendMQBean.setSendMqMessage(JSON.toJSONString(outCallApiLogBean));
            mqSendService.sendCommonMQMessage(requestSendMQBean);
        } catch (Exception e) {
            log.info("请求url异常:" + url);
        }
    }

    @Value("${ds_cosmic_userbase_push}")
    private String DS_COSMIC_USERBASE_PUSH;

    @Resource
    private MqSendService mqSendService;

}
